package com.mmlc.meimei.lichengkongjian.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.httputil.EntryPageName;
import com.androidnetframe.requestbean.GlobalRequestBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.brand.BrandInfoActivity;
import com.mmlc.meimei.lichengkongjian.classify.CategoryListActivity;
import com.mmlc.meimei.lichengkongjian.classify.FastGoodsDetailActivity;
import com.mmlc.meimei.lichengkongjian.classify.MeiQiFastBuyActivity;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.meimei.lichengkongjian.customview.AdDialog;
import com.mmlc.meimei.lichengkongjian.customview.VpSwipeRefreshLayout;
import com.mmlc.network.filter.BannerRequestFilter;
import com.mmlc.network.filter.HotBrandRequestFilter;
import com.mmlc.network.filter.HotGoodsRequestFilter;
import com.mmlc.network.filter.NewProductListRequestFilter;
import com.mmlc.network.responsebean.BannerResponseBean;
import com.mmlc.network.responsebean.HotBrandResponseBean;
import com.mmlc.network.responsebean.HotGoodsResponseBean;
import com.mmlc.network.responsebean.NewProductListResponseBean;
import com.mmlc.utils.AppUtils;
import com.mmlc.utils.PermissionUtils;
import com.mmlc.utils.UIUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;
import com.mmlc.widget.Indicator;
import com.mmlc.widget.LinearBannerLayout;
import com.mmlc.widget.MyGridView;
import com.mmlc.widget.ViewBannerPager;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends InterFragment implements ActivityCompat.OnRequestPermissionsResultCallback{

    private class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            buildRecommandUI();
        }
    }

    private MyBroadcastReceiver mReceiver;

    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;//权限请求码


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter("com.mmlc.meiqi.BaseRequestSuccess");
        getActivity().registerReceiver(mReceiver, filter);
    }

    @Override
    protected int initResourceID() {
        return R.layout.fragment_home;
    }

    DisplayMetrics metrics;

    @Override
    protected void initExtra() {
        super.initExtra();

        metrics = getResources().getDisplayMetrics();
    }

    ScrollView scv_home;
    ViewBannerPager banner;
    LinearLayout ll_tab_hot_content, ll_tab_brand_content;
    LinearBannerLayout lbl_tab_hot, lbl_tab_brand;
    MyGridView grid_category;
    RelativeLayout rl_home;
    ViewGroup rootView;
    Indicator mIndicator;
    TextView tv_hot,tv_brand;
    ImageView iv_product_preview,iv_product_preview1,iv_product_preview2,iv_fashion_info;
    //    TextView tv_sale_price, tv_quality_goods, tv_unique;
    MyGridView grid_recommend;
    VpSwipeRefreshLayout mRefreshLayout;
    private AdapterView.OnItemClickListener categoryItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), CategoryListActivity.class);
            intent.putExtra("category", categoryDatas.get(position).getName())
                    .putExtra("categoryId", categoryDatas.get(position).getUrl())
                    .putExtra("relation_pos_id", categoryDatas.get(position).getRelation_pos_id());
            startActivity(intent);

//            DataUtils.toJump(context,categoryDatas.get(position));
        }
    };
    private AdapterView.OnItemClickListener recommendItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), ProductInfoActivity.class);
            intent.putExtra("name", recommendDatas.get(position).getGoods_name())
                    .putExtra("goods_id", recommendDatas.get(position).getId());
            startActivity(intent);
        }
    };
    ArrayList<BannerResponseBean.AdListBean> categoryDatas = new ArrayList<>();
    ArrayList<NewProductListResponseBean.DataBean.GoodslistBean> recommendDatas;
    RecommendAdapter recommendAdapter;
    Handler handler = new Handler();

    boolean isRefresh;
    @Override
    protected void initView() {
        iv_product_preview=ViewUtils.getView(view,R.id.iv_product_preview);
        iv_product_preview1=ViewUtils.getView(view,R.id.iv_product_preview1);
        iv_product_preview2=ViewUtils.getView(view,R.id.iv_product_preview2);
        iv_fashion_info=ViewUtils.getView(view,R.id.iv_fashion_info);
        rootView = ViewUtils.getView(view,R.id.rootView);
        scv_home = ViewUtils.getView(view, R.id.scv_home);
        banner = ViewUtils.getView(view, R.id.banner);
        grid_category = ViewUtils.getView(view, R.id.grid_category);
        mIndicator = ViewUtils.getView(view, R.id.indicator_v);
        ll_tab_hot_content = ViewUtils.getView(view, R.id.ll_tab_hot_content);
        lbl_tab_hot = ViewUtils.getView(view, R.id.lbl_tab_hot);
        ll_tab_brand_content = ViewUtils.getView(view, R.id.ll_tab_brand_content);
        lbl_tab_brand = ViewUtils.getView(view, R.id.lbl_tab_brand);
        tv_brand = ViewUtils.getView(view,R.id.tv_brand);
        tv_hot = ViewUtils.getView(view,R.id.tv_hot);
        mRefreshLayout = ViewUtils.getView(view,R.id.refresh);
        mRefreshLayout.setColorSchemeColors(Color.RED);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isRefresh) {
                    isRefresh = true;
                    if (handler != null) {
                        handler = null;
                    }
                    handler = new Handler() {
                        int i = 0;

                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            if (msg.what == 0) {
                                i++;
                                if (i == 3) {
                                    mRefreshLayout.setRefreshing(false);
                                    isRefresh = false;
                                }
                            }

                        }
                    };
                    initInfo();
                }

            }
        });

//        tv_sale_price = ViewUtils.getView(view, R.id.tv_sale_price);
//        tv_quality_goods = ViewUtils.getView(view, R.id.tv_quality_goods);
//        tv_unique = ViewUtils.getView(view, R.id.tv_unique);
        grid_recommend = ViewUtils.getView(view, R.id.grid_recommend);
        recommendDatas = new ArrayList<>();
        recommendAdapter = new RecommendAdapter(context, recommendDatas);
        grid_recommend.setAdapter(recommendAdapter);
        grid_recommend.setOnItemClickListener(recommendItemClick);
    }

    @Override
    protected void initInfo() {
        initBanner();
        initHot();
        initBrand();
        buildRecommandUI();
        scv_home.smoothScrollTo(0, 0);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**************************************
     * TODO Banner start
     **************************************/
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);
        }
        if(dialog!=null){
            dialog.dismiss();
        }

        getActivity().unregisterReceiver(mReceiver);
    }
    boolean adFirst = true;
    BannerRequestFilter filter;
    List<BannerResponseBean.AdListBean> ad_list;
    List<BannerResponseBean.AdListBean> ad_listAll;
    private void initBanner() {
        filter = new BannerRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.method = "getad";
        filter.requestBean.page = "home";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BannerResponseBean>() {
            @Override
            public void onSuccess(BannerResponseBean result) {
                if(result!=null && adFirst){
                    BannerResponseBean.DataBean.SalesBean sales = result.getData().getSales();
                    if(null!=sales.getAd_list()&&sales.getAd_list().size()!=0) {
                        ad_list = sales.getAd_list();
                        initAd(sales.getAd_list());

                    }
                }

                banner.setDatas(result.getData().getBanner().getAd_list());
                lbl_tab_brand.setDatas(result.getData().getEvent_brand().getAd_list());
                lbl_tab_hot.setDatas(result.getData().getEvent_hot().getAd_list());
                if(result.getData().getUnderbanner().getAd_list()!=null && result.getData().getUnderbanner().getAd_list().size()>=2){
                    XUtils.image(iv_product_preview, result.getData().getUnderbanner().getAd_list().get(0).getImg());
                    XUtils.image(iv_product_preview1, result.getData().getUnderbanner().getAd_list().get(1).getImg());
                    XUtils.image(iv_product_preview2, result.getData().getUnderbanner().getAd_list().get(2).getImg());
                    XUtils.image(iv_fashion_info, result.getData().getUnderbanner().getAd_list().get(3).getImg());
                    ad_listAll=result.getData().getUnderbanner().getAd_list();
                }
                if (result.getData().getTwitter4().getAd_list() != null) {
                    categoryDatas.clear();
                    categoryDatas.addAll(result.getData().getTwitter4().getAd_list());
                    grid_category.setAdapter(new HomeCategoryAdapter(context, categoryDatas));
                    grid_category.setOnItemClickListener(categoryItemClick);
                }
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                handler.sendEmptyMessage(0);
            }
        });
    }
    /**
     * 广告弹窗
     */
    public AdDialog dialog;
    private void initAd(final List<BannerResponseBean.AdListBean> adList) {
        adFirst = false;
        dialog = new AdDialog(getActivity(),R.style.BuyDialogStyle,adList);
        dialog.show();
        dialog.setClicklistener(new AdDialog.ClickListenerInterface() {
            @Override
            public void doAd() {
                if(adList!=null && adList.size()>0){
                    String type = adList.get(0).getType();
                    String url = adList.get(0).getUrl();
                    Intent intent;
                    switch (type){
                        case "0"://跳转H5
                            intent = new Intent(context, WebViewActivity.class);
                            intent.putExtra("title", adList.get(0).getName())
                                    .putExtra("url", url)
                                    .putExtra("shareImageUrl", adList.get(0).getImg());
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getActivity().startActivity(intent);
                            break;
                        case "4"://跳转普通商品详情
                            intent = new Intent(context, ProductInfoActivity.class);
                            intent.putExtra("name", adList.get(0).getName()).putExtra("goods_id", url);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getActivity().startActivity(intent);
                            break;
                        case "41"://跳转直购商品详情
                            intent = new Intent(context, FastGoodsDetailActivity.class);
                            intent.putExtra("id", url);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getActivity().startActivity(intent);
                            break;
                        case "5"://商品列表页(分类)
                            intent = new Intent(context, CategoryListActivity.class);
                            intent.putExtra("category", adList.get(0).getName())
                                    .putExtra("categoryId", url)
                                    .putExtra("relation_pos_id", adList.get(0).getRelation_pos_id());
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getActivity().startActivity(intent);
                            break;
                        case "6"://商品列表页(品牌)
                            intent = new Intent(context, BrandInfoActivity.class);
                            intent.putExtra("name", adList.get(0).getName()).putExtra("brand_id", url);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getActivity().startActivity(intent);
                            break;
                    }


                }

                dialog.dismiss();
            }

            @Override
            public void doCancel() {
                dialog.dismiss();
            }
        });
    }

    void gotoActivity(BannerResponseBean.AdListBean adListBean){
        String type = adListBean.getType();
        String url = adListBean.getUrl();
        Intent intent;
        switch (type){
            case "0"://跳转H5
                intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("title", adListBean.getName())
                        .putExtra("url", url)
                        .putExtra("shareImageUrl", adListBean.getImg());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case "4"://跳转普通商品详情
                intent = new Intent(context, ProductInfoActivity.class);
                intent.putExtra("name", adListBean.getName()).putExtra("goods_id", url);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case "41"://跳转直购商品详情
                intent = new Intent(context, FastGoodsDetailActivity.class);
                intent.putExtra("id", url);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case "5"://商品列表页(分类)
                intent = new Intent(context, CategoryListActivity.class);
                intent.putExtra("category", adListBean.getName())
                        .putExtra("categoryId", url)
                        .putExtra("relation_pos_id", adListBean.getRelation_pos_id());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case "6"://商品列表页(品牌)
                intent = new Intent(context, BrandInfoActivity.class);
                intent.putExtra("name", adListBean.getName()).putExtra("brand_id", url);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case "7"://尖货尖是
                intent = new Intent(getActivity(), NewProductActivity.class);
//                intent.putExtra("category", "新品预览");
                startActivity(intent);
                break;
            case "8"://潮流资讯
                startActivity(new Intent(getActivity(), FashionInfoActivity.class));
                break;
            case "9":
                break;
            case "10":
                break;
            case "11":
                break;
            case "12":
                break;
            case "13":
                break;
        }
    }

    /**************************************
     * Banner end
     **************************************/
    /**************************************
     * TODO Hot Recommend start
     **************************************/

    private void initHot() {
        HotGoodsRequestFilter filter = new HotGoodsRequestFilter((BaseParentActivity) getActivity());
        if(isFirst) {
            filter.offerErrorParams(rootView);
            filter.isTransparence = true;
        }
        isFirst = false;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<HotGoodsResponseBean>() {
            @Override
            public void onSuccess(HotGoodsResponseBean result) {
                tv_hot.setVisibility(View.VISIBLE);
                displayHotGoods(result);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                handler.sendEmptyMessage(0);

            }
        });
    }
    List<HotGoodsResponseBean.DataBean> hotGoods = new ArrayList<>();
    boolean isOne = true;
    private void displayHotGoods(HotGoodsResponseBean result) {
        hotGoods.clear();
        hotGoods.addAll(result.getData());
        if(ll_tab_hot_content.getChildCount()> 0 ) {
            ll_tab_hot_content.removeAllViews();
        }
        for (int i = 0; i < hotGoods.size(); i++) {
            View view = context.getLayoutInflater().inflate(R.layout.item_tab_hot, null);
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            int px = (int)UIUtils.dp2px(16.66f);
            LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(2 * (display.getWidth()-px*2) / 7, LinearLayout.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(ll_params);
            ImageView iv_image = ViewUtils.getView(view, R.id.iv_buy_image);
            TextView tv_name = ViewUtils.getView(view, R.id.tv_name);
            TextView tv_price = ViewUtils.getView(view, R.id.tv_price);

            XUtils.image(iv_image, hotGoods.get(i).getImg_thumb(),
                    XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.FIT_CENTER).build());
            if(hotGoods.get(i).getGoods_name().length()>5){

                tv_name.setText(hotGoods.get(i).getGoods_name().substring(0,5)+"...");

            }else {

                tv_name.setText(hotGoods.get(i).getGoods_name());
            }
            tv_price.setText("¥ " + hotGoods.get(i).getPrice());

            final int index = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ProductInfoActivity.class);
                    intent.putExtra("goods_id", hotGoods.get(index).getId());
                    startActivity(intent);
                }
            });
            ll_tab_hot_content.addView(view);
        }

    }

    /**************************************
     * Hot Recommend end
     **************************************/

    /**************************************
     * TODO Brand Recommend start
     **************************************/

    private void initBrand() {
        HotBrandRequestFilter filter = new HotBrandRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.method = "hotBrand";
        if(isFirst) {
            filter.offerErrorParams(rootView);
            filter.isTransparence = true;
        }
        isFirst = false;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<HotBrandResponseBean>() {
            @Override
            public void onSuccess(HotBrandResponseBean result) {
                handler.sendEmptyMessage(0);
                tv_brand.setVisibility(View.VISIBLE);
                displayHotBrand(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                handler.sendEmptyMessage(0);
            }
        });
    }

    List<HotBrandResponseBean.DataBean> hotBrands = new ArrayList<>();

    private void displayHotBrand(HotBrandResponseBean result) {
        hotBrands.clear();
        hotBrands.addAll(result.getData());
        if(ll_tab_brand_content.getChildCount()> 0 ) {
            ll_tab_brand_content.removeAllViews();
        }
        for (int i = 0; i < hotBrands.size(); i++) {
            ImageView view = (ImageView) LayoutInflater.from(context).inflate(R.layout.item_tab_brand, null);
            LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(327 * metrics.widthPixels / 1080, 228 * metrics.heightPixels / 1920);
            view.setLayoutParams(ll_params);

            XUtils.image(view, hotBrands.get(i).getBrand_logo(),
                    XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_CROP).build());
            final int index = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), BrandInfoActivity.class);
                    intent.putExtra("name", hotBrands.get(index).getBrand_name())
                            .putExtra("brand_id", hotBrands.get(index).getId());
                    startActivity(intent);
                }
            });
            ll_tab_brand_content.addView(view);
        }

//        scv_home.scrollTo(0, 0);
    }

    /**************************************
     * Brand Recommend end
     **************************************/

    /**************************************
     * Recommend start
     **************************************/

    boolean isFirst = true;

    private void buildRecommandUI() {
        if (EntryPageName.globalRequestBean.getData() != null
                && EntryPageName.globalRequestBean.getData().getRecommend() != null) {
            Log.d("HOME", "uiget" + EntryPageName.globalRequestBean.getData().getRecommend().getHome().size());
            mHomeBeanList = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (GlobalRequestBean.DataBean.RecommendBean.HomeBean homeBean :
                    EntryPageName.globalRequestBean.getData().getRecommend().getHome()) {
                if (TextUtils.equals("2", homeBean.getType())) {
                    titles.add(homeBean.getName());
                    mHomeBeanList.add(homeBean);
                }
            }
            Log.d("HOME", "titles.size()" + titles.size());
            if (titles.size() > 1) {
                mIndicator.setTitles(titles);
                mIndicator.setHasBottom(false);
                mIndicator.setHasDivide(true);
                mIndicator.setVisibility(View.VISIBLE);
                mIndicator.setIndexChangeListener(new Indicator.onIndexChangeListener() {
                    @Override
                    public void onIndexSelected(int index) {
                        initRecommend(index);
                    }
                });
                initRecommend(0);
            } else
                mIndicator.setVisibility(View.GONE);

        }

    }

    private List<GlobalRequestBean.DataBean.RecommendBean.HomeBean> mHomeBeanList;


    private void initRecommend(int filtrate) {
        NewProductListRequestFilter filter = new NewProductListRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.recommend_id = mHomeBeanList.get(filtrate).getId();
        filter.requestBean.version = AppUtils.getVersionCode(getActivity())+"";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<NewProductListResponseBean>() {

            @Override
            public void onSuccess(NewProductListResponseBean result) {
                displayRecommend(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    List<NewProductListResponseBean.DataBean.GoodslistBean> recommendList;
    private void displayRecommend(NewProductListResponseBean result) {
        recommendList = result.getData().getGoodslist();
        if (recommendList != null) {
            recommendDatas.clear();
            recommendDatas.addAll(recommendList);
        }
        recommendAdapter.notifyDataSetChanged();

        if (isFirst) {
            scv_home.scrollTo(0, 0);
        }
    }

    /**************************************
     * Recommend end
     **************************************/
    @Override
    protected void initEvent() {
        super.initEvent();
        ViewUtils.getView(view, R.id.iv_search).setOnClickListener(this);
        ViewUtils.getView(view, R.id.iv_scan).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_product_preview).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_product_preview1).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_product_preview2).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_aulet).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_fashion_info).setOnClickListener(this);
        ViewUtils.getView(view, R.id.rl_wish_list).setOnClickListener(this);
/*
        ViewUtils.getView(view, R.id.tv_sale_price).setOnClickListener(this);
        ViewUtils.getView(view, R.id.tv_quality_goods).setOnClickListener(this);
        ViewUtils.getView(view, R.id.tv_unique).setOnClickListener(this);*/
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                 case PermissionUtils.CODE_CAMERA:
                    FragmentActivity activity = getActivity();
                    App.setActivity((BaseParentActivity)activity);
                    Intent intentscan = new Intent(getActivity(), WeChatCaptureActivity.class);
                    startActivity(intentscan);
                    break;
                default:
                    PermissionUtils.requestPermission(getActivity(), PermissionUtils.CODE_CAMERA, mPermissionGrant);
                    break;
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionsResult(getActivity(), requestCode, permissions, grantResults, mPermissionGrant);
    }



    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent;
        switch (v.getId()) {
            case R.id.iv_scan:
                PermissionUtils.requestPermission(getActivity(), PermissionUtils.CODE_CAMERA, mPermissionGrant);
                break;
//            case R.id.tv_title:
//                startActivity(new Intent(getActivity(), MeiQiFastBuyActivity.class));
            case R.id.iv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.rl_product_preview:
//                intent = new Intent(getActivity(), NewProductActivity.class);
////                intent.putExtra("category", "新品预览");
//                startActivity(intent);
//                ToastUtils.showToast("此功能正在开发中，敬请期待");
                gotoActivity(ad_listAll.get(0));
                break;
            case R.id.rl_product_preview1:
                gotoActivity(ad_listAll.get(1));
                break;
            case R.id.rl_product_preview2:
                gotoActivity(ad_listAll.get(2));
                break;
            case R.id.rl_aulet:
                intent = new Intent(getActivity(), CategoryListActivity.class);
                intent.putExtra("category", "力诚空间奥莱").putExtra("categoryId", EntryPageName.globalRequestBean.getData().getBest_lolever());
                startActivity(intent);
                break;
            case R.id.rl_fashion_info:
//                startActivity(new Intent(getActivity(), FashionInfoActivity.class));
                gotoActivity(ad_listAll.get(3));
                break;
            case R.id.rl_wish_list:
                startActivity(new Intent(getActivity(), MeiQiFastBuyActivity.class));
//                openWhisList();
                break;
         /*   case R.id.tv_sale_price://特价专享
                resetNav(tv_sale_price);
                initRecommend(0);
                recommendAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_quality_goods://品牌推荐
                resetNav(tv_quality_goods);
                initRecommend(1);
                recommendAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_unique://限量孤品
                resetNav(tv_unique);
                initRecommend(2);
                recommendAdapter.notifyDataSetChanged();
                break;*/
        }
    }

    private void openWhisList() {
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("url", "http://act.lolever.com/vow/index.html");
        intent.putExtra("title", "心愿清单");
        intent.putExtra("shareImageUrl", "http://manager.lolever.com/assets/admin/images/logo.png");
        startActivity(intent);
    }


 /*   private void resetNav(TextView textview) {
        if (textview.getId() == R.id.tv_sale_price)
            tv_sale_price.setTextColor(getResources().getColor(R.color.text_black));
        else tv_sale_price.setTextColor(getResources().getColor(R.color.text_grey));
        if (textview.getId() == R.id.tv_quality_goods)
            tv_quality_goods.setTextColor(getResources().getColor(R.color.text_black));
        else tv_quality_goods.setTextColor(getResources().getColor(R.color.text_grey));
        if (textview.getId() == R.id.tv_unique)
            tv_unique.setTextColor(getResources().getColor(R.color.text_black));
        else tv_unique.setTextColor(getResources().getColor(R.color.text_grey));
    }*/


}
